package com.wn.ticket.security;

import com.wn.ticket.entity.User;
import com.wn.ticket.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * @创建人 NST
 * @创建时间 2022/8/17
 * @描述
 */
@Slf4j
@Component
public class FrontUserDetailsService implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.getByPhoneNumber(username);
        if (user == null) {
            throw new UsernameNotFoundException("账号不存在");//抛出异常会由自定义的登录过滤器中的验证失败处理器处理
        }
        ArrayList<SimpleGrantedAuthority> authorities = new ArrayList<>();
        return new FrontUser(user.getId(), username, user.getPassword(), authorities);
    }
}
